package cn.jit.ssm.demo;

import cn.jit.ssm.utils.CompareUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Cecurio
 * @create: 2017-12-15 21:21
 **/
public class Demo4 {
    public static void exportXlsExcel(String[] titles, String fileName, List<Object[]> dataList) {
        if (CompareUtil.isEmpty(titles)) return;
        if (CompareUtil.isEmpty(dataList)) return;
        if (CompareUtil.isEmpty(fileName)) fileName = "templete";

        int columnNum = titles.length;

        // 新建工作簿
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();

        // 新建工作表
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();

        // 设置第一行
        HSSFRow firstRow = hssfSheet.createRow(0);

        HSSFCell tempCell = null;
        for (int i = 0; i < columnNum; i++) {
            tempCell = firstRow.createCell(i);
            tempCell.setCellValue(titles[i]);
        }

        // 放置数据
        for (int i = 0; i < dataList.size(); i++) {
            Object[] obj = dataList.get(i);//遍历每个对象
            HSSFRow row = hssfSheet.createRow(i + 1);//创建所需的行数
            for (int j = 0; j < obj.length; j++) {
                HSSFCell cell = null;   //设置单元格的数据类型
                cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);
                if (!"".equals(obj[j]) && obj[j] != null) {
                    cell.setCellValue(obj[j].toString());                       //设置单元格的值
                }
            }
        }

        // 让列宽随着导出的列长自动适应
        // 其实就是遍历每一列的所有的行的字符内容，找出最大的长度，然后这一列的宽度都设置为此值
        for (int colNum = 0; colNum < columnNum; colNum++) {
            int columnWidth = hssfSheet.getColumnWidth(colNum) / 256;
            for (int rowNum = 0; rowNum < hssfSheet.getLastRowNum(); rowNum++) {
                HSSFRow currentRow;
                //当前行未被使用过
                if (hssfSheet.getRow(rowNum) == null) {
                    currentRow = hssfSheet.createRow(rowNum);
                } else {
                    currentRow = hssfSheet.getRow(rowNum);
                }
                if (currentRow.getCell(colNum) != null) {
                    HSSFCell currentCell = currentRow.getCell(colNum);
                    if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                        int length = currentCell.getStringCellValue().getBytes().length;
                        if (columnWidth < length) {
                            columnWidth = length;
                        }
                    }
                }
            }
            if (colNum == 0) {
                hssfSheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
            } else {
                hssfSheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
            }
        }

        // 导出文件
        try {
            OutputStream outputStream = new FileOutputStream(new File("D:\\Tmp\\excel\\" + fileName + ".xls"));
            hssfWorkbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            System.out.println("导出成功！");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String[] titles = new String[]{"bookId", "title", "image", "price", "url", "isbn13", "summary"};
        List<Object[]> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String[] contents = new String[]{"bookId", "title", "image", "price", "url", "isbn13", "summary"};
            for (int j = 0; j < contents.length; j++) {
                contents[j] += (i + 1);
            }
            list.add(contents);
        }
        exportXlsExcel(titles, null, list);
    }
}
